#ifndef CQUATERNION_H
#define CQUATERNION_H


/* Copyright (C) 2011 Riccardo Marson

   This file is part of OpenPool, an open-source game of pool.

   Permission is hereby granted, free of charge, to any person obtaining a
   copy of this software and associated documentation files (the "Software"),
   to deal in the Software without restriction, including without limitation
   the rights to use, copy, modify, merge, publish, distribute, sublicense,
   and/or sell copies of the Software, and to permit persons to whom the
   Software is furnished to do so, subject to the following conditions:

   The above copyright notice and this permission notice shall be included
   in all copies or substantial portions of the Software.

   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
   OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
   FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
   RICCARDO MARSON BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
   AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
   CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

  See Readme.rtf for further information.

*/

#include "CVector3.h"

//! \brief Quaternion
class CQuaternion
{
public:

    CQuaternion(); //! empty constructor
    CQuaternion(float a, float x, float y, float z); //! constructor. vector components are initialised with x, y, z
    CQuaternion(float components[4]); //! constructor. takes a pointer to float. nice to have when initialising vectors from rows of CMatrix4x4
    CQuaternion(float scalar, CVector3 vector, bool isRotation); //! constructor, takes a scalar part and a vector part to initialize the quaternion, or a constructs a quaternion rotation, angle in radians 
	CQuaternion(const CQuaternion &source); //! copy constructor

    ~CQuaternion(); //! deconstructor

    float& operator() (const unsigned short i);//! outputs mData[i]. i must be between 0 and 2
    CQuaternion operator+ (CQuaternion quat); //! sum of 2 quaternions
    CQuaternion operator+ (const float scalar); //! sum of a quaternion and a scalar constant
    CQuaternion operator- (CQuaternion quat); //! subtraction of 2 quaternions
    CQuaternion operator- (const float scalar); //! subtraction of scalar constant
    CQuaternion operator* (CQuaternion quat); //! quaternion multiplication
	CQuaternion operator* (CVector3 vector); //! quaternion x vector multiplication
    CQuaternion operator* (const float scalar); //! multiplication by a scalar value
    CQuaternion& operator= (CQuaternion quat); //! assignement
    bool operator== (CQuaternion quat);//! equality test
    bool operator!= (CQuaternion quat); //! inequality test

    inline float Norm(); //! returns the quaternion norm
	void Normalize(); //! tranforms to unit quaternion
    void SetComponents(float a, float x, float y, float z); //! sets (or re-sets) the quaternion components to a, x, y, z
	void SetComponents(float a, CVector3 vector); //! sets (or re-sets) the quaternion components to a, vector.x, vector.y, vector.z
    void SetRotationComponents(float angle, CVector3 rotationAxis); //! constructs a quaternion rotation, angle in radians
	float* GetComponents(); //! provides in output the pointer to mData
    
	float& GetScalarPart(); //! returns mData[0]
    CVector3 GetVectorPart(); //! returns CVector3(mData[1], mData[2], mData[3])
	CQuaternion Conjugate(); //! returns CQuaternion(mData[0], -mData[1], -mData[2], -mData[3])
	CVector3 RotateVector(CVector3 vector); //! performs a vector rotation

	friend std::ostream & operator << (std::ostream &out, CQuaternion &quat);

	// TODO 
	// 1) return angle and unit vector from quaternion
	// 2) return rotation matrix from quaternion
	// 3) (optional) compute quaternion for euler rotations. Note: requires an enumeration for the possible sequences of euler rotations.

private:

    float mData[4];
};


#endif // CQUATERNION_H
